package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.IParkServ;
import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.Park;
import com.gudaomai.wuye.entity.ParkInput;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/park")
public class ParkCtrl extends BaseController {
	
	@Autowired
	IParkServ parkService;
	
	/** 
	 * add a new Park
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if  Park with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The park settings page, click the add button, add park")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addPark(@RequestBody ParkInput parkInput) throws Exception {
		RetEntity entity = new RetEntity();

		if(parkService.isExist(parkInput.getCode())){
			// The park already exists in the database
			
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
					getMessage(MsgConstant.RET_PARK_ADD_FAILURE));
		} 
		else{
			// Create new park entity
			Park parkEntity = new Park();
			// copy all user inputs to the new park entity
			BeanUtils.copyProperties(parkInput, parkEntity);
			
			// Insert the park entity into the database
			parkService.insert(parkEntity);
			
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_PARK_ADD_SUCCESS));
		}	
		
		return entity;
	}
	
	/** 
	 * query Park information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The park settings page, click the query button, query park info")	
	@GetMapping("/id/{id}")
	public Park getParkById(@PathVariable("id") String id) throws Exception{
				
		Park park = parkService.selectById(id);
		
		park.setRetCode(CommonConstant.RESULT_CODE_OK);
		park.setRetMessage(getMessage(MsgConstant.RET_PARK_SELECT_SUCCESS));
		return park;
	}
	
	/** 
	 * update the existing Park information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update Park information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The park settings page, click the modify button, update park")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RetEntity updatePark(@RequestBody ParkInput parkInput) throws Exception{
				
		    RetEntity entity = new RetEntity();
			
			// update the park entity in the database
			// Create new park entity
			Park parkEntity = new Park();
			// copy all user inputs to the new park entity
			BeanUtils.copyProperties(parkInput, parkEntity);
			
			parkService.updateById(parkEntity);
			
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_PARK_UPDATE_SUCCESS));
			
			return entity;
	}
	
	
	/** 
	 * delete the existing Park
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete Park information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The park settings page, click the delete button, delete Park")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delParkById(@PathVariable("id") String id) throws Exception{
		
		RetEntity entity = new RetEntity();
		
        parkService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_PARK_DELETE_SUCCESS));
		return entity;
	}
}
